package com.zhkj.txg.module.mine.ui

import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.zhkj.lib.base.ViewModelActivity
import com.zhkj.lib.http.HttpResponseStatus
import com.zhkj.txg.R
import com.zhkj.txg.module.mine.adapter.NotificationAdapter
import com.zhkj.txg.module.mine.vm.NotificationViewModel
import kotlinx.android.synthetic.main.activity_guide_list.*

class SystemNotificationActivity : ViewModelActivity<NotificationViewModel>() {
    override fun setViewModel(): NotificationViewModel = createViewModel()
    override fun statusTitleBar(): View? = titleHelp
    override val layoutResId: Int = R.layout.activity_guide_list

    private val notificationAdapter = NotificationAdapter().apply {
        loadMoreModule?.setOnLoadMoreListener {
            viewModel.notification()
        }
    }

    override fun initUI() {
        titleHelp.setTitleTextContent(getString(R.string.system_notification))
        with(rvHelp) {
            layoutManager = LinearLayoutManager(this@SystemNotificationActivity)
            adapter = notificationAdapter
        }
        notificationAdapter.setEmptyView(R.layout.layout_empty_data)
        refreshHelp.setOnRefreshListener { initData() }
    }

    override fun initData() {
        viewModel.notification(true)
    }

    override fun initViewModel() {
        viewModel.notificationLiveData.observe(this, Observer { response ->
            when (response.status) {
                HttpResponseStatus.Status.START -> Unit
                HttpResponseStatus.Status.SUCCESS -> response.response?.let {
                    // 判断加载状态
                    if (!it.loadMore) refreshHelp.finishRefresh()
                    else if (it.hasMore) notificationAdapter.loadMoreModule?.loadMoreComplete()
                    else notificationAdapter.loadMoreModule?.loadMoreEnd()
                    // 数据填充
                    if (it.loadMore) notificationAdapter.addData(it.data)
                    else notificationAdapter.setNewData(it.data)
                }
                HttpResponseStatus.Status.FAILURE -> {
                    response.exception?.printStackTrace()
                    toast(response.statusTip)
                    if (viewModel.page > 1) notificationAdapter.loadMoreModule?.loadMoreFail()
                    else refreshHelp.finishRefresh(false)
                }
            }
        })
    }
}